<?php

namespace app\admin\controller;

use app\BaseController;
use app\dic\DicSite;
use think\App;



class Tasksite extends BaseController
{

    protected $model = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 更新域名
     */
    public function index()
    {
        $iscustom = table('site')
            ->where(['site_type' => DicSite::$site_type_1, 'status' => DicSite::$status_normal])
            ->select()->toarray();
        if ($iscustom) {
            foreach ($iscustom as $v) {
                $this->deal_site($v);
            }
        }
        return success('更新成功');
    }

    public function deal_site($iscustom)
    {
        $url = $iscustom['site_url'] . "/admin/api/2021-07/shop.json";
        $data = geturl($url, $iscustom['apikey'], $iscustom['apisecret']);
        if ($data) {
            $data = json_decode($data, true);
            if (isset($data['shop'])) {
                //更新一级域名
                table('site')->where(['id' => $iscustom['id']])->update(['site_name' => $data['shop']['name']]);
            }
        }
    }

    //测试二级域名有效性
    public function test_site()
    {
        $url =  "https://justfitme.net/admin/api/2021-07/shop.json";
        $apikey = '56b355dc2f80e4ce2acbbd1f79342bd1';
        $apisecret = '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';
        $data = geturl($url, $apikey, $apisecret);
        $data = json_decode($data, true);
        halt($data);
    }
}
